package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"testGin/entity"
	"testGin/tool"
)

func Love(c *gin.Context) {
	u_v := entity.GetUserVideoCollectLoveUpload(c)
	if u_v.Vid <= 0 || u_v.Uid <= 0 {
		c.JSON(http.StatusOK, tool.R{
			403, "请求参数错误", nil,
		})
		return
	}
	GetDB().Table("love").Select("uid", "vid").Create(&u_v)
	GetRedisConn().Do("incr", "L:"+strconv.Itoa(u_v.Vid))
	c.JSON(http.StatusOK, tool.R{
		200, "喜欢成功", nil,
	})
}

func LoveCancel(c *gin.Context) {
	u_v := entity.GetUserVideoCollectLoveUpload(c)
	if u_v.Vid <= 0 || u_v.Uid <= 0 {
		c.JSON(http.StatusOK, tool.R{
			403, "请求参数错误", nil,
		})
	}
	GetDB().Table("love").Where("uid = ? and vid = ?", u_v.Uid, u_v.Vid).Delete(&u_v)
	GetRedisConn().Do("decr", "L:"+strconv.Itoa(u_v.Vid))
	c.JSON(http.StatusOK, tool.R{
		200, "取消点赞成功", nil,
	})
}

// 查询用户点赞列表
func LoveListByUid(c *gin.Context) {
	u_v := entity.GetUserVideoCollectLoveUpload(c)
	if u_v.Uid <= 0 {
		c.JSON(http.StatusForbidden, tool.R{
			403, "请求参数错误", nil,
		})
		return
	}
	var u_v_s []entity.UserVideoCollectLoveUpload
	GetDB().Table("love").Where("uid = ?", u_v.Uid).Find(&u_v_s)
	var arr []int
	for i := 0; i < len(u_v_s); i++ {
		arr = append(arr, u_v_s[i].Vid)
	}
	var videos []entity.Video
	GetDB().Where(map[string]interface{}{"id": arr}).Find(&videos)
	videos = VideosFill(videos)
	c.JSON(http.StatusOK, tool.R{
		200, "ok", videos,
	})
}
